package final

import (
	"context"
	"net/http"
	"time"
)

type HttpPool struct {
	pool chan *http.Client
}

func NewHttpPool(size int) *HttpPool {
	pool := make(chan *http.Client, size)
	for i := 0; i < size; i++ {
		pool <- &http.Client{
			Timeout: 5 * time.Second,
		}
	}
	return &HttpPool{
		pool: pool,
	}
}

func (p *HttpPool) Get(ctx context.Context) *http.Client {
	return <-p.pool
}

func (p *HttpPool) Release(ctx context.Context, client *http.Client) {
	p.pool <- client
}

func (p *HttpPool) Close() {
	close(p.pool)
}
